package com.fypg.module.medical.service.outpatientorderrefund;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.fypg.module.medical.controller.admin.outpatientorderrefund.vo.*;
import com.fypg.module.medical.dal.dataobject.outpatientorderrefund.OutpatientOrderRefundDO;
import com.fypg.framework.common.pojo.PageResult;
import com.fypg.framework.common.pojo.PageParam;
import com.fypg.framework.common.util.object.BeanUtils;

import com.fypg.module.medical.dal.mysql.outpatientorderrefund.OutpatientOrderRefundMapper;

import static com.fypg.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.fypg.framework.common.util.collection.CollectionUtils.convertList;
import static com.fypg.framework.common.util.collection.CollectionUtils.diffList;
import static com.fypg.module.medical.enums.ErrorCodeConstants.*;

/**
 * 门诊订单退款信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class OutpatientOrderRefundServiceImpl implements OutpatientOrderRefundService {

    @Resource
    private OutpatientOrderRefundMapper outpatientOrderRefundMapper;

    @Override
    public String createOutpatientOrderRefund(OutpatientOrderRefundSaveReqVO createReqVO) {
        // 插入
        OutpatientOrderRefundDO outpatientOrderRefund = BeanUtils.toBean(createReqVO, OutpatientOrderRefundDO.class);
        outpatientOrderRefundMapper.insert(outpatientOrderRefund);
        // 返回
        return outpatientOrderRefund.getId();
    }

    @Override
    public void updateOutpatientOrderRefund(OutpatientOrderRefundSaveReqVO updateReqVO) {
        // 校验存在
        validateOutpatientOrderRefundExists(updateReqVO.getId());
        // 更新
        OutpatientOrderRefundDO updateObj = BeanUtils.toBean(updateReqVO, OutpatientOrderRefundDO.class);
        outpatientOrderRefundMapper.updateById(updateObj);
    }

    @Override
    public void deleteOutpatientOrderRefund(String id) {
        // 校验存在
        validateOutpatientOrderRefundExists(id);
        // 删除
        outpatientOrderRefundMapper.deleteById(id);
    }

    @Override
        public void deleteOutpatientOrderRefundListByIds(List<String> ids) {
        // 校验存在
        validateOutpatientOrderRefundExists(ids);
        // 删除
        outpatientOrderRefundMapper.deleteByIds(ids);
        }

    private void validateOutpatientOrderRefundExists(List<String> ids) {
        List<OutpatientOrderRefundDO> list = outpatientOrderRefundMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(OUTPATIENT_ORDER_REFUND_NOT_EXISTS);
        }
    }

    private void validateOutpatientOrderRefundExists(String id) {
        if (outpatientOrderRefundMapper.selectById(id) == null) {
            throw exception(OUTPATIENT_ORDER_REFUND_NOT_EXISTS);
        }
    }

    @Override
    public OutpatientOrderRefundDO getOutpatientOrderRefund(String id) {
        return outpatientOrderRefundMapper.selectById(id);
    }

    @Override
    public PageResult<OutpatientOrderRefundDO> getOutpatientOrderRefundPage(OutpatientOrderRefundPageReqVO pageReqVO) {
        return outpatientOrderRefundMapper.selectPage(pageReqVO);
    }

}